// 私有属性
// 1. 借助symobol
let x = Symbol()
class A {
    constructor(a) {
        this[x] = a
    }

    getA() {
        return this[x];
    }
}
// 2. 借助闭包
(() => {
    let x = {}
    class A {
        constructor(a) {
            this[x] = a
        }
        getA() {
            return this[x];
        }
    }
    return A
})()



// 寄生组合继承
function Father() {}
function Son(...args) {
    Father.call(this, ...args)
}
function inherit(Son, Father) {
    let newObj = Object.create(Father.prototype)
    Son.prototype = newObj;
    Son.prototype.constrctor = Son;
}
inherit(Son, Father)


// vue Extend
Vue.extend = function (params) {
    const Super = this

    let Sub = function VueConponent(options) {
        this._init(optiosn)
    }

    Sub.prototype = Object.create(Super.prototype)
    Sub.prototype.constructor = Sub;

    Sub['super'] = Super;
    Sub.extend = Super.extend;

    return Sub
}


// set的内部实现
// https://www.cnblogs.com/baiyangyuanzi/p/6689796.html
/*js集合set类的实现*/
function Set() {
    this.dataStore = [];
    this.add = add;//新增元素
    this.remove = remove;//删除元素
    this.size = size;//集合的元素个数
}
function add(data) {
    if (this.dataStore.indexOf(data) < 0) {
        this.dataStore.push(data);
        return true;
    }
    else {
        return false;
    }
}
function remove(data) {
    var pos = this.dataStore.indexOf(data);
    if (pos > -1) {
        this.dataStore.splice(pos,1);
        return true;
    }
    else {
        return false;
    }
}
function size() {
    return this.dataStore.length;
}

/*测试例子：求补集。属于集合cis,不属于集合it*/
var cis = new Set();
var it = new Set();
cis.add("Clayton");
cis.add("Jennifer");
cis.add("Danny");
it.add("Bryan");
it.add("Clayton");
it.add("Jennifer");


// symbol 类型实现
const MySymbol = ((x) => {
    let cache = new Map();

    function fn() {
        // 判断不能new
        if (this instanceof fn) {
            throw new Error('wri')
        }
        cache.set(x, {})
        return {}
    }
    fn.for = function (key) {
        if (cache.has(key)) {
            return cache.get(key)
        }
        return {}
    }

    fn.keyfor = function (val) {
        for (const [k, v] of cache) {
            if (v == val) {
                return k
            }
        }
    }
    return fn

})(x)
console.log(new MySymbol("1"))


// setTimeout 实现定时器 Interval
function mySetInterval(fn, time) {
    function wrapFn(...args) {
        setTimeout(wrapFn, time)
        fn.call(this, ...args)
    }
    setTimeout(wrapFn, time)
}

mySetInterval(() => { console.log(1) }, 2 * 1000)

// 实现new
function myNew(fn, ...args) {
    let newObj = Object.create(fn.prototype)
    let result = fn.call(newObj, ...args)
    result instanceof Object ? result : newObj
}

// 实现instance
function myInstacneOf(obj, Obj) {
    let objProto = obj.__proto__
    let ObjProto = Obj.prototype
    while (true) {
        if (objProto === null) {
            return false
        }
        if (objProto === ObjProto) {
            return true
        }
        objProto = objProto.__proto__
    }
}
function fn(){}
let a = new fn()
console.log(a instanceof Array)

